<template>
  <view class="shipping-address">
    <customHeader class="shipping-address-title" title="收货地址" />
    <addressCard
      v-if="addressInfo"
      class="address-card"
      :address-info="addressInfo"
      @edit="goEdit"
    />
    <template v-else>
      <ranking-empty text="暂无地址" :bg-img="address_empty" />
      <finish-button class="add-address" :width="690" @click="handleAdd">
        添加地址
      </finish-button>
    </template>
  </view>
</template>

<script>
import "./index.scss";
import Taro from "@tarojs/taro";
import rankingEmpty from "_c/ranking-empty/index";
import customHeader from "@/components/custom-header";
import finishButton from "_c/finish-button/index";
import addressCard from "_c/address-card/index";
import { address_empty } from "@/libs/image-url";
import { address } from "_api/mine";

export default {
  name: "ShippingAddress",
  components: {
    rankingEmpty,
    finishButton,
    addressCard,
    customHeader
  },
  data() {
    return {
      address_empty,
      addressInfo: undefined
    };
  },
  onShow() {
    this.getAddress();
  },
  methods: {
    getAddress() {
      address()
        .then(r => {
          this.addressInfo = r.data;
        })
        .catch(err => {
          Taro.showToast({
            title: err.message,
            icon: "none",
            duration: 2000
          });
        });
    },
    handleAdd() {
      Taro.navigateTo({
        url: "/pages/address-edit/index?isEdit=0"
      });
    },
    goEdit() {
      Taro.navigateTo({
        url: "/pages/address-edit/index?isEdit=1"
      });
    }
  }
};
</script>
